import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# List of spline types
B = [57, 58]

# Loop over each B-spline type
for spline_type in B:
        # Construct the file name for the current spline and N value
        file_name = f'spline_{spline_type}.csv'

        # Read the data from the CSV file
        try:
            data = pd.read_csv(file_name, header=None)  # Reading the CSV data
        except FileNotFoundError:
            print(f"File {file_name} not found. Skipping.")
            continue

        # Create a new figure for each plot
        plt.figure(figsize=(10, 8))

        # Plot the spline interpolation data
        plt.plot(data[0], data[1], label=f'{spline_type}')

        # Plot the exact function f(x) = 1 / (1 +  x^2)
        x_exact = np.linspace(1, 10, 400)
        y_exact = 1 / (1 + x_exact**2)
        plt.plot(x_exact, y_exact, 'k--', label='Exact function')

        # Adding the title, labels, and grid
        plt.title(f'Problem B: {spline_type}')
        plt.xlabel('X')
        plt.ylabel('f(X)')
        plt.grid(True)

        # Add legend to the plot
        plt.legend()

        # Save the plot as a PNG file
        plt.savefig(f'../figure/B_{spline_type}.png')

        # Show the plot
        plt.show()
